<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use App\Model\Enum\RiskRuleMchRefTypeEnum;
use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\RiskRuleMchRefService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
use App\Middleware\AdminJWTAuthMiddleware;

/**
 * Class RiskRuleMchRefController
 * 商户风控
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/riskRuleMchRef")
 * @Middleware(AdminJWTAuthMiddleware::class)
 */
class RiskRuleMchRefController extends AdminBaseController {

    /**
     * @Inject()
     * @var RiskRuleMchRefService
     */
    protected $RiskRuleMchRefService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 商户风控/列表
     * @PostMapping(path = "list")
     */
    public function list (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'page'  => 'required|integer|min:1',
            'limit' => 'required|integer|min:1',
        ]);

        $params = [
            'page'  => $request->post('page'),
            'limit' => $request->post('limit'),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($params, $search, 'mchNo');
        CustomUtils::xzxAddSearch($params, $search, 'type');

        // TODO mchIdList权限控制

        // $params['mchIdList'] = null;

        return $this->RiskRuleMchRefService->list($params);
    }

    /**
     * 商户风控/列表
     * @PostMapping(path = "info")
     */
    public function info (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo' => 'required',
            'type'  => 'required',
        ]);

        $params = [
            'mchNo' => $request->post('mchNo'),
            'type'  => $request->post('type'),
        ];

        // TODO mchIdList权限控制
        // $params['mchIdList'] = null;

        return $this->RiskRuleMchRefService->info($params);

    }

    /**
     * 选项列表
     * @PostMapping(path = "typeOptions")
     *
     * @param RequestInterface $request
     *
     * @return array
     */
    public function typeOptions (RequestInterface $request) {

        $this->_validOrFail($request->all(), [// 'id' => 'required',
        ]);

        $param = [// 'id' => $request->post('id'),
        ];

        return $this->RiskRuleMchRefService->typeOptions($param);
    }

    /**
     * 设置关联
     * @PostMapping(path = "setBind")
     *
     * @param RequestInterface $request
     *
     * @return array
     * @throws \Exception
     */
    public function setBind (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo'        => 'required',
            'type'         => 'required',
        ]);

        $ext = $this->request->getAttribute("ext");

        $params = [
            'mchNo'        => $request->post('mchNo'),
            'collectionId' => $request->post('collectionId', null),
            'type'         => (int) $request->post('type'),
        ];

        $params['type'] = RiskRuleMchRefTypeEnum::born($params['type'])->value();

        $params['createBy'] = $ext['user']['username'];

        // TODO mchIdList权限控制

        return $this->RiskRuleMchRefService->setBind($params);
    }

}
